package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Permission;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Set;

/***
 *权限接口
 */
public interface PermissionDao {
    /***
     * 根据角色id查询权限
     * @param roleId
     * @return
     */
    Set<Permission> findPermissionByRoleId(Integer roleId);



    /**
     * @author TanLin
     * 分页查询
     * @param queryString
     * @return
     */
    Page<Permission> findPagePermission(String queryString);

    /**
     * 添加权限
     * @param permission
     */
    @Insert("INSERT INTO t_permission (name,keyword,description) VALUES (#{name},#{keyword},#{description})")
    void add(Permission permission);

    /**
     * 编辑回显
     * @param id
     * @return
     */
    @Select("select * from t_permission where id = #{id}")
    Permission findById(Integer id);

    /**
     * 编辑提交
     * @param permission
     */
    @UpdateProvider(type = PermissionDao.update.class,method = "updatePermission")
    void edit(Permission permission);



    class update{
        public String updatePermission(Permission permission){
            SQL sql = new SQL().UPDATE("t_permission");
            if (permission.getName() != null && !"".equals(permission.getName())){
                sql.SET("name=#{name}");
            }
            if (permission.getKeyword() != null && !"".equals(permission.getKeyword())){
                sql.SET("keyword=#{keyword}");
            }
            if (permission.getDescription() != null && !"".equals(permission.getDescription())){
                sql.SET("description=#{description}");
            }
            sql.WHERE("id=#{id}");
            return sql.toString();
        }
    }

    /**
     * 删除前查看id是否有关联
     * @param id
     * @return
     */
    @Select("select count(role_id) from t_role_permission where permission_id = #{id}")
    Long findbyIdRole_permission(Integer id);


    /**
     * 删除
     * @param id
     */
    @Delete("delete from t_permission where id = #{id}")
    void delete(Integer id);


    /**
     * 查询全部
    * @param
    * @return java.util.List<com.itheima.pojo.Permission>
    */
    List<Permission> findAll();

}
